package com.zr.utils;

/**
 * @author 杜雨萌
 * @date 2022/10/29
 * @version 2022.1
 */
public class NormalizeUtil {


    /**
     * 归一化：将二维数组的值转化为0-1区间内，最小值不能为0
     *
     * @param seq
     * @return
     * @author 杜雨萌
     */
    public static double[][] getNormalize(double[][] seq) {
        double max = seq[0][0];
        double min = Integer.MAX_VALUE;
        //获得最大值和最小值
        for (int i = 0; i < seq.length; i++) {
            for (int j = 0; j < seq[i].length; j++) {
                if (seq[i][j] != 0 && seq[i][j] < min) {
                    min = seq[i][j];
                }
                if (seq[i][j] > max) {
                    max = seq[i][j];
                }
            }
        }
        //归一化的结果二维数组
        double[][] result = new double[seq.length][seq.length];
        //将传参的二维数组归一化
        for (int i = 0; i < seq.length; i++) {
            for (int j = 0; j < seq[i].length; j++) {
                if (seq[i][j] != 0) {
                    result[i][j] = (seq[i][j] - min) / (max - min);
                } else {
                    result[i][j] = 0;
                }

            }
        }
        return result;
    }


}
